import { Upload, message, Button } from "antd";
import { UploadOutlined } from "@ant-design/icons";
import { API } from "../../axios";
import { FC } from "react";

interface IProps {
    getFiles(paegNo?: number, pageSize?: number): void;
}

const AddFile: FC<IProps> = function ({ getFiles }) {
    const props = {
        name: "file",
        className: "uplodfiles",
        accept: ".txt", // 文件类型
        action: API, // 上传地址
        headers: {
            authorization: "authorization-text"
        },
        multiple: true, // 支持多文件
        showUploadList: false, // 隐藏展示列表
        onChange: (info: any) => {
            if (info.file.status !== "uploading") {
                info.fileList.forEach((item: any, index: number) => {
                    if (item.type !== "text/plain") {
                        let NoText = info.fileList.splice(index, 1);
                        message.warning(`${NoText.name} 文件不是 .txt !!!`);
                    }
                });
            }
            if (info.file.status === "done") {
                message.success(`${info.file.name} 上传成功!`, 3);
                getFiles();
            } else if (info.file.status === "error") {
                message.error(`${info.file.name} 上传失败！`, 0);
            }
        }
    };

    return (
        <>
            <Upload {...props}>
                <Button icon={<UploadOutlined />}>上传文件</Button>
            </Upload>
        </>
    );
};

export default AddFile;
